'''
Date: 2023-12-04 10:59:35
Author: WT-W-PC
LastEditors: WT-W-PC
LastEditTime: 2023-12-07 17:08:23
Description: 
获取最优链接，获取失败则等待一段指数时间后再试
登陆
登陆后获取剩余天数、剩余流量以及优惠活动等信息
签到并格式化返回消息
推送结果
'''

from notify import Message
from juziyunrobot import JuziyunRobot
import utils
from utils import JsonConfig
import math

JUZIYUN_LINK_LIST_NAME = 'juziyun_link_list'
BARK_KEY_NAME = 'bark_key'
CONFIG_DIR = 'juziyun.json'
EMAIL_NAME = 'email'
PASSWORD_NAME = 'passwd'
TIMEOUT = 2

# 检查环境变量是否为空
def inspectenv(inspection_dict):
    result = True
    details = ''
    for key, value in inspection_dict.items():
        if value == None:
            result = False
            details = details + f'{key}配置为空，'
    if not result:
        details = details + '请检查配置文件。'
    return result, details
        
        
if __name__ == "__main__":
    # 初始化环境信息
    log_msg = f'初始化环境信息...'
    utils.logging.info(log_msg)
    config = JsonConfig(CONFIG_DIR)
    if config.init():
        bark_key = config.read(BARK_KEY_NAME)      
        juziyun_link_list = config.read(JUZIYUN_LINK_LIST_NAME)
        email = config.read(EMAIL_NAME)
        passwd = config.read(PASSWORD_NAME)
        
        waiting_dict = {
            JUZIYUN_LINK_LIST_NAME: juziyun_link_list,
            EMAIL_NAME : email,
            PASSWORD_NAME : passwd
        }
        
        # 初始化环境信息
        log_msg = f'获取配置信息...'
        utils.logging.info(log_msg)
        next_step, details = inspectenv(waiting_dict)
        
        if not next_step:
            utils.logging.error(details)
            # 创建bark通知
            if bark_key != None:
                temp_msg = f'创建Bark通知实例...'
                utils.logging.info(temp_msg)
                correspondent = Message(bark_key)
                log_msg = 'juziyun签到：' + details
                correspondent.push(log_msg)
        else:            
            # 创建juziyun实例
            log_msg = f'创建juziyun实例...'
            utils.logging.info(log_msg)
            juziyun = JuziyunRobot(juziyun_link_list, email, passwd)
            
            log_msg = f'juziyun连通性测试...'
            utils.logging.info(log_msg)
            next_step = False
            for i in range(1,6):
                if juziyun.getlinkbysmoothaccess(TIMEOUT):
                    next_step = True
                    break
                
                # 最后一次不等待
                if i != 5:
                    breaking_time = 60 * math.exp(i)
                    log_msg = f'休息 {str(breaking_time)} 分钟后再试...'
                    utils.logging.info(log_msg)
                    utils.pause(breaking_time)
            try:
                if next_step:
                    log_msg = f'登陆juziyun...'
                    utils.logging.info(log_msg)
                    juziyun.login()
                    
                    log_msg = f'登陆用户页并获取信息...'
                    utils.logging.info(log_msg)
                    juziyun.acquireinfo()
                    
                    log_msg = f'进行签到...'
                    utils.logging.info(log_msg)
                    juziyun.signin()
                    
                    log_msg = f'更新配置文件信息...'
                    utils.logging.info(log_msg)
                    juziyun_link_dict_new = {
                        JUZIYUN_LINK_LIST_NAME: juziyun.juziyun_url_list_new
                    }
                    config.write(juziyun_link_dict_new)
                    
                    # 通知用户结果
                    if bark_key != None:
                        temp_msg = f'创建Bark通知实例...'
                        utils.logging.info(temp_msg)
                        correspondent = Message(bark_key)
                        log_msg = 'juziyun签到：' + juziyun.signin_msg + '\n'
                        log_msg = log_msg + f'剩余会员天数：{juziyun.remaining_days}\n'
                        log_msg = log_msg + f'剩余流量：{juziyun.surplus_flow}'
                        if juziyun.special_offers != '':
                            log_msg = log_msg + f'\n优惠信息：{juziyun.special_offers}'
                        if juziyun.discount_time != '':
                            log_msg = log_msg + f'\n{juziyun.discount_time}'
                        correspondent.push(log_msg)
                        
                    log_msg = 'juziyun签到完成！'
                    utils.logging.info(log_msg)
                else:
                    log_msg = f'juziyun连通性测试失败，请检查网络。'
                    utils.logging.info(log_msg)
                    # 创建bark通知
                    if bark_key != None:
                        temp_msg = f'创建Bark通知实例...'
                        utils.logging.info(temp_msg)
                        correspondent = Message(bark_key)
                        log_msg = 'juziyun签到：' + log_msg
                        correspondent.push(log_msg)
            except Exception as e:
                utils.logging.error(e)
                if isinstance(e, utils.CommonException) and bark_key != None:
                    temp_msg = f'创建Bark实例并通知用户...'
                    utils.logging.info(temp_msg)
                    correspondent = Message(bark_key)
                    log_msg = 'juziyun签到：' + str(e)
                    correspondent.push(log_msg)                 
    else:
        log_msg = f'初始化环境信息失败，请检查系统环境。'
        utils.logging.error(log_msg)